package com.hoas.quickstart.threads.chapter03.chapter03_1_11_4;

import java.util.ArrayList;
import java.util.List;

public class MyStack {

    private List list = new ArrayList<>();

    synchronized public void push() {
        try {
            while(list.size() == 1) {
                this.wait();
            }
            list.add("anyString=" + Math.random());
//            this.notify();//会出现异常
            this.notifyAll();
            System.out.println("push=" + list.size());

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    synchronized public String pop() {
        String returnVal = "";
        try {
            while (list.size() == 0) {
                System.out.println("pop操作中的：" + Thread.currentThread().getName() + " 线程是wait状态" );
                this.wait();
            }

            returnVal = "" + list.get(0);
            this.list.remove( 0 );
//            this.notify();
            this.notifyAll();
            System.out.println("pop=" + list.size());

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return returnVal;
    }

}
